package highscore;

import highscore_lib.HighScoreWebInterface;
import highscore_lib.HighScoreWebInterfaceException;
import java.util.Iterator;
import java.util.Map.Entry;

public class WebHighScoreDatabaseAdapter extends HighScoreDatabase {

	private HighScoreWebInterface webFace = new HighScoreWebInterface();
	
	private int id = webFace.initialize();
	private HighScore scores = new HighScore(); 
	private HighScore nums = new HighScore(); 

	public HighScore retrieveHighScore() {
		try {
			while(webFace.checkConnection(id) == false){
				webFace.connect(id); 
			}
		}  catch (HighScoreWebInterfaceException ex) {
			System.out.println("High Score Web Interface Connection Exception! " + ex);
			ex.printStackTrace(); 
		}
		try {
			while(webFace.checkHighScoreExists(id) == false){
				webFace.createNewHighScore(id); 
			}
		} catch (HighScoreWebInterfaceException ex) {
			System.out.println("High Score does not exist! " + ex);
			ex.printStackTrace();
			return new HighScore();
		}
		Iterator<Entry<String, Integer>> it = scores.getMap().entrySet().iterator(); 
		while(it.hasNext()){
			Entry<String, Integer> entry = it.next(); 
			String str = getHighScoreWrapper(id,  entry.getKey());
			int playerName = str.indexOf("for");
			int the = str.indexOf("the"); 
			int is = str.indexOf("is"); 
			int scoreSpot = str.indexOf("10"); 
			String name = str.substring(playerName, the-playerName); 
			String score = str.substring((scoreSpot + 1) - is); 
			int sc = Integer.parseInt(score); 
			nums.addScore(name, sc); 
		}
		return nums; 
		//returning empty highscore in every case is incorrect, what in case when write happened and John Doe is already on the list ?
		//you should call webFace.getHighScore(id,"John Doe") and return proper HighScore object, this would require remembering
		//the name in write() method because retrieveHighScore() does not take any parameters so you don't know the names that you
		//should use when calling webFace.getHighScore.
		//save names to field map<string, integer> and call upon it to iterate through and retrieve values and put into new highscore object. 
	}

	//This is a log of an error that I got, it seems that getHighScore will throw an exception in every
	//case when name does not exists, you will have put addHighScore in appropriate catch block.
	
	/*HighScoreWebInterfaceException highscore_lib.HighScoreWebInterfaceException: Person doesn't exists
Would you like to play another game? (y/n)
highscore_lib.HighScoreWebInterfaceException: Person doesn't exists
	at highscore_lib.HighScoreWebInterface.getHighScore(HighScoreWebInterface.java:61)
	at highscore.WebHighScoreDatabaseAdapter.write(WebHighScoreDatabaseAdapter.java:44)
	at game.Engine.playGame(Engine.java:57)
	at MainClass.main(MainClass.java:16)*/

	
	public void write(HighScore h){
		Iterator<Entry<String, Integer>> it = h.getMap().entrySet().iterator(); 
		while(it.hasNext()){
			Entry<String, Integer> entry = it.next();
			try{
				if(getHighScoreWrapper(id, entry.getKey()) == null){
					webFace.addHighScore(id, entry.getKey(), entry.getValue()); 
					scores.addScore(entry.getKey(), entry.getValue()); 
				} else {
					String s = webFace.getHighScore(id, entry.getKey()); 
					int playerName = s.indexOf("for");
					int the = s.indexOf("the"); 
					int is = s.indexOf("is"); 
					int scoreSpot = s.indexOf("10"); 
					String name = s.substring(playerName, the-playerName); 
					String score = s.substring((scoreSpot + 1) - is); 
					int sc = Integer.parseInt(score); 
				    webFace.addHighScore(id, name, sc + entry.getValue()); 
				    scores.addScore(entry.getKey(), entry.getValue());
				}
			} catch(HighScoreWebInterfaceException ex){
				System.out.println("HighScoreWebInterfaceException " + ex); 
				ex.printStackTrace(); 
			}
		}
	}
	
	private String getHighScoreWrapper(int id, String key){
		try{
			return webFace.getHighScore(id, key); 
		} catch (HighScoreWebInterfaceException e){ 
			return "";
		}
	}
}

